#!/bin/sh

#desc:Show cameras
#package:odvideo
#type:local

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later
# Isidro Catalan <skarvin@gmail.com>



# ----------------------------------------------------------------------------
# desc: Camera control
# author: Isidro Catalan <skarvin@gmail.com>, http://www.opendomo.org
# date: September 2009
#
# CHANGES:
# - Jun 2011: (Daniel Lerch) - record button

# ----------------------------------------------------------------------------


RECORD_SCRIPT=/usr/local/opendomo/services/syscript/recordCamera.sh


if ! test -f "/etc/opendomo/videoConfVars.conf"; then
   cp /etc/opendomo/videoConfVars.conf.orig /etc/opendomo/videoConfVars.conf
fi
. "/etc/opendomo/videoConfVars.conf"

if ! test -f "/etc/opendomo/video/common.conf"; then
   cp /etc/opendomo/video/common.conf.orig /etc/opendomo/video/common.conf
fi


# Motion Handler
. "$MTN_HANDLER_PATH"

#res=`isRunning`

#if [ "$res" = "0" ]; then
#	echo "#WARN Motion is not running"
#	echo
#	exit 1
#fi


# Start or stop recording
if ! test -z "$2"
then
	if test "$2" = "on" 
	then
		$RECORD_SCRIPT $1 start 2>/dev/null &
	else
		$RECORD_SCRIPT $1 stop 2>/dev/null &
	fi
fi



echo "#> Show cameras"
echo "list:`basename $0`	iconlist"

FAKEPID="/var/run/odvision_access.pid"
ACCESS_ENABLED=`cat $FAKEPID` 2>/dev/null
if test "$ACCESS_ENABLED" != "1"; then
	echo "#WARN Access to cameras is disabled"
	echo "actions:"
	echo "	setSystemState.sh	Activate access"
	echo
	exit 0
fi

cd "$VIDEO_CONF_PATH"

if test -z "$1"; then
	for conf_file in *.conf; do
		if test -f "$conf_file"; then
			. ./$conf_file
			cam_name=`basename "$conf_file" | cut -d"." -f1`	
			if [ "$cam_name" = "common" ]; then
				continue;
			fi

			spaces_name=`echo "$NAME" | tr "+" " "`
			url_stream=`echo "/cgi-bin/od.cgi?resource=$cam_name"`	

			if test "`$RECORD_SCRIPT $cam_name status 2>/dev/null`" = "recording" 
			then
				bstatus="recording";
			else
				bstatus="stopped"
			fi

			if test -z "$SELECTEDZONE" || test "$ZONE" = "$SELECTEDZONE"; then
			 	echo "	$cam_name	$spaces_name	stream $bstatus	$url_stream"
			 	#echo "	$cam_name	$spaces_name	image ip	$url_stream"
				#echo "	$cam_name	Rec	subcommand[on,off]	$bstatus"
			fi
		fi
	done
	echo "actions:"
	echo "	manageCameras.sh	Manage cameras"
else
	if test -f "$1.conf"; then
		conf_file="$1.conf"
		. ./$conf_file
		cam_name=`basename "$conf_file" | cut -d"." -f1`	
		if [ "$cam_name" = "common" ]; then
			continue;
		fi

		spaces_name=`echo "$NAME" | tr "+" " "`
		url_stream=`echo "/cgi-bin/od.cgi?resource=$cam_name"`	

		if test "`$RECORD_SCRIPT $cam_name status 2>/dev/null`" = "recording" 
		then
			bstatus="recording";
		else
			bstatus="stopped"
		fi	
		echo "	$cam_name	$spaces_name	streamzoomed $bstatus	$url_stream"
	fi
	echo "actions:"
	echo "	cameraShow.sh	Back"
fi

if test -x /usr/local/opendomo/services/tools/browseCapturesAI.sh; then     
	echo -n "
	browseCapturesAI.sh	Browse captures"
fi
echo 
